﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace NppPluginNET
{
    public class PluginExtensionLoader
    {
        private const string EXTENSION_PATH = "./Language/";

        private const string SPEECH_EXTENSION_FILE = "speech.xml";

        private Dictionary<string,ExtensionLanguage> extensionLanguages;

        private Dictionary<string, string> speechExtensions;

        public PluginExtensionLoader()
        {
            extensionLanguages = new Dictionary<string,ExtensionLanguage>();
            speechExtensions = new Dictionary<string, string>();
        }

        public void LoadSpeechExtensions()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(EXTENSION_PATH + SPEECH_EXTENSION_FILE);
            XmlElement model = doc.DocumentElement;

            foreach (XmlNode node in model.GetElementsByTagName("extension"))
            {
                string name = node.Attributes["name"].InnerText;
                speechExtensions[name] = node.InnerText;
            }
        }

        public void LoadLanguageExtensions()
        {
            var files = Directory.GetFiles(EXTENSION_PATH, "*xml");

            foreach(var file in files)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(file);
                XmlElement model = doc.DocumentElement;
                if(model.Name == "language")
                {
                    XmlNode name = model.GetElementsByTagName("name")[0];

                    ExtensionLanguage newLanguage = new ExtensionLanguage(name.InnerText);

                    if(model.GetElementsByTagName("if").Count > 0)
                        newLanguage.IfText = model.GetElementsByTagName("if")[0].InnerText.Trim();

                    if (model.GetElementsByTagName("ifelse").Count > 0)
                        newLanguage.IfElseText = model.GetElementsByTagName("ifelse")[0].InnerText.Trim();

                    if (model.GetElementsByTagName("while").Count > 0)
                        newLanguage.WhileText = model.GetElementsByTagName("while")[0].InnerText.Trim();

                    if (model.GetElementsByTagName("for").Count > 0)
                        newLanguage.ForText = model.GetElementsByTagName("for")[0].InnerText.Trim();

                    if (model.GetElementsByTagName("class").Count > 0)
                        newLanguage.ClassText = model.GetElementsByTagName("class")[0].InnerText.Trim();

                    if (model.GetElementsByTagName("ctor").Count > 0)
                        newLanguage.ConstructorText = model.GetElementsByTagName("ctor")[0].InnerText.Trim();

                    if (model.GetElementsByTagName("function").Count > 0)
                        newLanguage.FunctionText = model.GetElementsByTagName("function")[0].InnerText.Trim();

                    if (model.GetElementsByTagName("gettersetter").Count > 0)
                        newLanguage.GetterSetterText = model.GetElementsByTagName("gettersetter")[0].InnerText.Trim();

                    extensionLanguages[newLanguage.Name] = newLanguage;
                }
            }
        }

        public Dictionary<string, ExtensionLanguage> ExtensionLanguages
        {
            get { return extensionLanguages; }
            set { extensionLanguages = value; }
        }

        public Dictionary<string, string> SpeechExtensions
        {
            get { return speechExtensions; }
            set { speechExtensions = value; }
        }
        
    }
}
